﻿using System;
using System.Configuration;
using System.Data;
using System.Collections;
using digit.Core;


namespace SMS.SmManager
{
    public class MsgIn_85Manager
    {
        #region CONSTRUCTORS
        static MsgIn_85Manager()
        {
            _data = new MsgIn_85Data(Util.GetConnectionString("SMSC"));
            _error = "";
        }
        #endregion

        #region ATTRIBUTES
        private static MsgIn_85Data _data;
        private static string _error;
        #endregion

        #region PROPERTIES
        public static string ErrorMessage
        {
            get
            {
                return _error;
            }
        }

        #endregion

        #region METHODS
        public static bool Insert(long requestID, string mobileNo, string serviceID, string msgContent, DateTime requestTime, byte status)
        {
            _error = "";
            try
            {
                object[] values = new object[6];
                values[0] = requestID;
                values[1] = mobileNo;
                values[2] = serviceID;
                values[3] = msgContent;
                values[4] = requestTime;
                values[5] = status;

                return _data.Insert(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Insert(MsgIn_85Info info)
        {
            _error = "";
            try
            {
                object[] values = new object[6];
                values[0] = info.RequestID;
                values[1] = info.MobileNo;
                values[2] = info.ServiceID;
                values[3] = info.MsgContent;
                values[4] = info.RequestTime;
                values[5] = info.Status;

                return _data.Insert(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Update(long msgInID, long requestID, string mobileNo, string serviceID, string msgContent, DateTime requestTime, byte status)
        {
            _error = "";
            try
            {
                object[] values = new object[7];
                values[0] = msgInID;
                values[1] = requestID;
                values[2] = mobileNo;
                values[3] = serviceID;
                values[4] = msgContent;
                values[5] = requestTime;
                values[6] = status;

                return _data.Update(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool UpdateError(long MsgInID, string Content)
        {
            _error = "";
            try
            {
                object[] values = new object[2];
                values[0] = MsgInID;
                values[1] = Content;

                return _data.UpdateError(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Update(MsgIn_85Info info)
        {
            _error = "";
            try
            {
                object[] values = new object[7];
                values[0] = info.MsgInID;
                values[1] = info.RequestID;
                values[2] = info.MobileNo;
                values[3] = info.ServiceID;
                values[4] = info.MsgContent;
                values[5] = info.RequestTime;
                values[6] = info.Status;

                return _data.Update(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Delete(long msgInID)
        {
            _error = "";
            try
            {
                object[] values = { msgInID };
                return _data.Delete(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Delete(MsgIn_85Info info)
        {
            _error = "";
            try
            {
                object[] values = { info.MsgInID };
                return _data.Delete(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static MsgIn_85Info Select(long msgInID)
        {
            _error = "";
            try
            {
                object[] values = { msgInID };
                DataTable dTable = _data.Select(values);
                if (dTable.Rows.Count < 1) return null;


                MsgIn_85Info info = new MsgIn_85Info(dTable.Rows[0]);
                return info;
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SelectAll()
        {
            _error = "";
            try
            {
                return _data.SelectAll();
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SelectAllNetwork()
        {
            _error = "";
            try
            {
                return _data.SelectAllNetwork();
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SelectAllMessageError()
        {
            _error = "";
            try
            {
                return _data.SelectAllMessageError();
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static ArrayList SelectAllMsgProcess()
        {
            _error = "";
            try
            {
                DataTable dt = _data.SelectAllMsgProcess();

                if (dt.Rows.Count < 0) return null;

                ArrayList arrMsgin = new ArrayList();

                foreach (DataRow dr in dt.Rows)
                {
                    MsgIn_85Info msgIn = new MsgIn_85Info();

                    msgIn.MsgInID = long.Parse(dr["MsgInID"].ToString());
                    msgIn.RequestID = int.Parse(dr["RequestID"].ToString());
                    msgIn.RequestTime = Convert.ToDateTime(dr["RequestTime"].ToString());
                    msgIn.MobileNo = dr["MobileNo"].ToString();
                    msgIn.ServiceID = dr["ServiceID"].ToString();
                    msgIn.MsgContent = dr["MsgContent"].ToString();
                    msgIn.Status = byte.Parse(dr["Status"].ToString());

                    arrMsgin.Add(msgIn);
                }
                return arrMsgin;
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static bool UpdateStatus(long msgInID, byte status)
        {
            _error = "";
            try
            {
                object[] values = { msgInID, status };
                return _data.UpdateStatus(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool UpdateError(long msgInID, string mobile, string content, byte status)
        {
            _error = "";
            try
            {
                object[] values = { msgInID, mobile, content, status };
                return _data.UpdateError(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool InsertError(string RequestID, DateTime ResponseTime, string Error, string Type)
        {
            _error = "";
            try
            {
                object[] values = new object[4];
                values[0] = RequestID;
                values[1] = ResponseTime;
                values[2] = Error;
                values[3] = Type;

                return _data.InsertError(Error);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        #endregion
    }
}